package com.bootdo.shop.controller;

import java.util.*;

import javax.servlet.http.HttpServletRequest;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.bootdo.shop.domain.ShopDO;
import com.bootdo.shop.service.ShopService;
import com.bootdo.system.domain.UserDO;
import com.bootdo.common.utils.PageUtils;
import com.bootdo.common.utils.Query;
import com.bootdo.common.utils.R;
import com.bootdo.common.utils.ShiroUtils;

/**
 * 门店表
 * 
 * @author chglee
 * @email 1992lcg@163.com
 * @date 2018-04-26 14:34:54
 */
 
@Controller
@RequestMapping("/shop/shop")
public class ShopController {
	@Autowired
	private ShopService shopService;
	
	@GetMapping()
	@RequiresPermissions("shop:shop:shop")
	String Shop(){
	    return "shop/shop/shop";
	}
	
	@ResponseBody
	@GetMapping("/list")
	@RequiresPermissions("shop:shop:shop")
	public PageUtils list(@RequestParam Map<String, Object> params){
		//查询列表数据
        Query query = new Query(params);
		List<ShopDO> shopList = shopService.list(query);
		int total = shopService.count(query);
		PageUtils pageUtils = new PageUtils(shopList, total);
		return pageUtils;
	}
	
	@GetMapping("/add")
	String add(){  
	    return "shop/shop/add";
	}

	@GetMapping("/edit/{shopId}")
//	@RequiresPermissions("shop:shop:edit")
	String edit(@PathVariable("shopId") Long shopId,Model model){
		ShopDO shop = shopService.get(shopId);
		model.addAttribute("shop", shop);
	    return "shop/shop/edit";
	}
	
//	/**
//	 * 保存
//	 */
//	@ResponseBody
//	@PostMapping("/save")
//	public R save( ShopDO shop){
//		if(shopService.save(shop)>0){
//			return R.ok();
//		}
//		return R.error();
//	}
	/**
	 * 修改
	 */
	@ResponseBody
	@RequestMapping("/update")
	public R update( ShopDO shop){
		shopService.update(shop);
		return R.ok();
	}
	
	/**
	 * 删除
	 */
	@PostMapping( "/remove")
	@ResponseBody
	public R remove( Long shopId){
		if(shopService.remove(shopId)>0){
		return R.ok();
		}
		return R.error();
	}
	
	/**
	 * 删除
	 */
	@PostMapping( "/batchRemove")
	@ResponseBody
	public R remove(@RequestParam("ids[]") Long[] shopIds){
		shopService.batchRemove(shopIds);
		return R.ok();
	}
	
	
	/**
	 * 保存
	 */
	@ResponseBody
	@PostMapping("/save")
	public R saveP( ShopDO shop){
		UserDO u = ShiroUtils.getUser();
		shop.setShopId(shop.getShopId());
		shop.setShopName(shop.getShopName());
		shop.setShopAddress(shop.getShopAddress());
		shop.setShopPrincipal(shop.getShopPrincipal());
		shop.setShopPhone(shop.getShopPhone());
		shop.setShopZphone(shop.getShopZphone());
		shop.setProviderId(u.getProviderId());
		if(shopService.save(shop)>0){
			return R.ok();
		}
		return R.error();
	}
	
	
	@ResponseBody
	@PostMapping("/shopName")
	public Map<String, Object> shopName(String keyword){
		UserDO userDO = ShiroUtils.getUser();
		List<Map<String, String>> list = new ArrayList<>();
		 Map<String,String> m = new HashMap<>();
		 List<ShopDO> strings = shopService.getShop(keyword, userDO.getProviderId());
		if(strings.size()==0) {
			m.put("title", "暂无此门店");
			list.add(m);
		}
		 for (ShopDO string : strings) {
			 m = new HashMap<>();
			 m.put("title", string.getShopName());
			 m.put("address", string.getShopAddress());
			 m.put("phone", string.getShopPhone());
			 m.put("linkname", string.getShopPrincipal());
			 list.add(m);
		}

		 Map<String,Object> map = new HashMap<>();
		map.put("data", list);
		 return map;
	}
	
}
